/*
 * Copyright (C) 2008-2009 the original author or authors
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *         http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.code.liquidform;

import com.google.code.liquidform.internal.AliasFactory;
import com.google.code.liquidform.internal.FrameworkObjectFactory;
import com.google.code.liquidform.internal.managedobjects.FrameworkManagedObject;

/**
 * Provides support for inserting parameter placeholders in queries. Indexed and
 * named parameters are supported.
 */
public final class Parameters {

	private Parameters() {
		// utility class
	}

	private static FrameworkObjectFactory factory = FrameworkObjectFactory.INSTANCE;

	/**
	 * Insert a reference to the num<sup>th</sup> indexed parameter.
	 * 
	 * @param <T>
	 *            the java type of this expression
	 * @param num
	 *            the index number of parameter to use (indices are 1-based)
	 * @param clazz
	 *            you have to explicitly define the type of the resulting
	 *            expression, to help with type safety
	 * @return
	 */
	public static <T> T param(final int num, Class<T> clazz) {
		T o = factory.makeNew(clazz, false);
		AliasFactory.remember(o, new FrameworkManagedObject() {
			@Override
			public String getTargetRepresentation() {
				return "?" + num;
			}
		});
		return o;
	}

	/**
	 * Insert a reference to a named parameter.
	 * 
	 * @param <T>
	 *            the java type of this expression
	 * @param name
	 *            the name of the parameter to use
	 * @param clazz
	 *            you have to explicitly define the type of the resulting
	 *            expression, to help with type safety
	 * @return
	 */
	public static <T> T param(final String name, Class<T> clazz) {
		T o = factory.makeNew(clazz, false);
		AliasFactory.remember(o, new FrameworkManagedObject() {
			@Override
			public String getTargetRepresentation() {
				return ":" + name;
			}
		});
		return o;
	}

}
